package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.Question;
import com.coffeeloversupportlibrary.databinding.QuestionItemBinding;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/14 11:13
 * @projectName CoffeeLoverSupportLibrary
 * @className QuestionAdapter.java
 * @description TODO
 */
public class QuestionAdapter extends RecyclerView.Adapter {
    private QuestionItemBinding questionItemBinding;
    private List<Question> dataSource;

    public QuestionAdapter(List<Question> dataSource) {
        this.dataSource = dataSource;
    }

    public List<Question> getDataSource() {
        return dataSource;
    }

    public void setDataSource(List<Question> dataSource) {
        this.dataSource = dataSource;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.question_item,parent,false);
        questionItemBinding= DataBindingUtil.bind(view);
        return new QuestionViewHolder(questionItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Glide.with(((QuestionViewHolder)holder).itemView.getContext())
                .load(dataSource.get(position).getQuestionContentImage())
                .into(((QuestionViewHolder)holder).questionItemBinding.questionContentImage);

    }

    @Override
    public int getItemCount() {
        return dataSource.size();
    }

    static class QuestionViewHolder extends RecyclerView.ViewHolder{
        QuestionItemBinding questionItemBinding;
        public QuestionViewHolder(@NonNull QuestionItemBinding questionItemBinding) {
            super(questionItemBinding.getRoot());
            this.questionItemBinding=questionItemBinding;
        }
    }
}
